//
//  lib_arithmetic.h
//  c2dx_sample
//
//  Created by qibinyi on 9/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef c2dx_sample_lib_arithmetic_h
#define c2dx_sample_lib_arithmetic_h

//#include "cocos2d.h"
#include "ccMacros.h"


void quicksort (int* array, int loIndex, int hiIndex)
{
    //  loIndex is the lower index, hi is the upper index
    //  hiIndex the region of array that is to be sorted
    int i=loIndex, j=hiIndex, h;
    int x=array[(loIndex+hiIndex)/2];
    
    //  partition
    do
    {    
        while (array[i]<x) i++; 
        while (array[j]>x) j--;
        if (i<=j)
        {
            h=array[i]; array[i]=array[j]; array[j]=h;
            i++; j--;
        }
    } while (i<=j);
    
    //  recursion
    if (loIndex<j) quicksort(array, loIndex, j);
    if (i<hiIndex) quicksort(array, i, hiIndex);
    
}

void quicksort_string (const char** array, int loIndex, int hiIndex)
{
    //  loIndex is the lower index, hi is the upper index
    //  hiIndex the region of array that is to be sorted
    int i=loIndex, j=hiIndex;
    const char* h;
    const char* x=array[(loIndex+hiIndex)/2];
    
    //  partition
    do
    {    
        while ( strcmp(array[i],x)< 0 ) i++; 
        while ( strcmp(array[j],x)> 0 ) j--;
        if (i<=j)
        {
            h=array[i]; array[i]=array[j]; array[j]=h;
            i++; j--;
        }
    } while (i<=j);
    
    //  recursion
    if (loIndex<j) quicksort_string(array, loIndex, j);
    if (i<hiIndex) quicksort_string(array, i, hiIndex);
    
}



int calcAngle(float x , float y ) {
	if (x==0) {
		if (y >=0) {
			return 90;
		}
		else {
			return 270;
		}
	}
	else  {
		return CC_RADIANS_TO_DEGREES(atanf(  y / x )) + (x>0?0:180 ) ;
		
	}
}


#endif
